package co.fitstart.admin.entity.search;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.UserSourceStatus;

/**
 * 
 * @author mohanlan
 *
 */
public class SourceSearch implements QueryBuilder {
    
    private String name;
    
    private String mobile;
    
    private Long site;
    
    private Boolean visited;
    
    private Boolean ordered;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date occurStart;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date occurEnd;
    
    private String userSourceStatus;
    
    private Long userSourceType;
    
    private List<Site> sites;
    
    private Order order;
    
    private Filter filter;
    
    /**
     * default constructor
     */
    public SourceSearch() {
        order = new Order("createTime", "DESC");
    }

    /*f
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {

        if(filter == null || refresh) {
            filter = new Filter().addSentence("userSource.site IN (:sites)", "sites", sites)
                                 .addEquals("userSource.site.id", site)
                                 .addEquals("userSource.visited", visited)
                                 .addEquals("userSource.ordered", ordered)
                                 .addEquals("userSource.userSourceType.id", userSourceType)
                                 .addLike("userSource.name", name)
                                 .addLike("userSource.mobile", mobile)
                                 .addDateTimeBetween("occurDate", occurStart, occurEnd);
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(this.userSourceStatus)) {
            	filter.addEquals("userSource.userSourceStatus", UserSourceStatus.valueOf(userSourceStatus));
            }
            
            
        }

        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM UserSource userSource WHERE userSource.disabled = false " + getPreparedFilter(false).getClauseSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the site
     */
    public Long getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Long site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
    
    /**
	 * @return the visited
	 */
	public Boolean getVisited() {
		return visited;
	}
	
	/**
	 * @param visited the visited to set
	 */
	public void setVisited(Boolean visited) {
		this.visited = visited;
	}
	
	/**
	 * @return the occurEnd
	 */
	public Date getOccurEnd() {
		return occurEnd;
	}
	
	/**
	 * @param occurEnd the occurEnd to set
	 */
	public void setOccurEnd(Date occurEnd) {
		this.occurEnd = occurEnd;
	}
	
	/**
	 * @return the occurStart
	 */
	public Date getOccurStart() {
		return occurStart;
	}
	
	/**
	 * @param occurStart the occurStart to set
	 */
	public void setOccurStart(Date occurStart) {
		this.occurStart = occurStart;
	}
	
	/**
	 * @return the ordered
	 */
	public Boolean getOrdered() {
		return ordered;
	}
	
	/**
	 * @param ordered the ordered to set
	 */
	public void setOrdered(Boolean ordered) {
		this.ordered = ordered;
	}
	
	/**
	 * @return the userSourceStatus
	 */
	public String getUserSourceStatus() {
		return userSourceStatus;
	}
	
	/**
	 * @param userSourceStatus the userSourceStatus to set
	 */
	public void setUserSourceStatus(String userSourceStatus) {
		this.userSourceStatus = userSourceStatus;
	}
	
	/**
	 * @return the userSourceType
	 */
	public Long getUserSourceType() {
		return userSourceType;
	}
	
	/**
	 * @param userSourceType the userSourceType to set
	 */
	public void setUserSourceType(Long userSourceType) {
		this.userSourceType = userSourceType;
	}
}
